package com.chixing.commons;

import com.chixing.pojo.Myorder;
import com.chixing.service.IMyorderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderDelayCancel {
    @Autowired
    private IMyorderService myorderService;
    @RabbitHandler
    @RabbitListener(queues = "delayQueue")
    public void orderCancel(Myorder myorder , Message message , Channel channel){
        try {
            ServerResponse response = myorderService.getById(myorder.getMyorderId());
            Myorder myorder1 = (Myorder)response.getData();
            if(myorder1.getMyorderStatus()==0) {
                myorder1.setMyorderStatus(2);
                ServerResponse serverResponse = myorderService.updateMyorder(myorder1);
                System.out.println(serverResponse.getReason());
            }
            System.out.println(myorder1);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
